package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.*;
import com.antler.smt.portal.mapper.*;
import com.antler.smt.response.admin.GetOrderLogisticsResp;
import com.antler.smt.response.admin.GetUserListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetExcellentCaseResp;
import com.antler.smt.response.portal.GetOrderCommentListResp;
import com.antler.smt.service.portal.ExcellentCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * 首页优秀案例详情
 *
 * @author zhangyi
 * @date 2018/5/8 16:04
 */
@Service
public class ExcellentCaseServiceImpl implements ExcellentCaseService {


    @Autowired
    private RequirementEntityMapper requirementEntityMapper;

    @Autowired
    private OrderEntityMapper orderEntityMapper;

    @Autowired
    private SupplierEntityMapper supplierEntityMapper;

    @Autowired
    private MemberEntityMapper memberEntityMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private OrderLogisticsInfoMapper orderLogisticsInfoMapper;

    @Autowired
    private OrderCommentInfoMapper orderCommentInfoMapper;

    @Autowired
    private PurchaserEntityMapper purchaserEntityMapper;

    @Autowired
    private AttachmentEntityMapper attachmentEntityMapper;

    @Autowired
    private OrderCommentPictureInfoMapper orderCommentPictureInfoMapper;

    /**
     * 获取优秀订单详情
     *
     * @param orderId
     * @return
     */
    @Override
    public Result<GetExcellentCaseResp> getExcellentCase(Long orderId) {

        Result result = null;

        //优秀订单返回实体
        GetExcellentCaseResp getExcellentCaseResp = new GetExcellentCaseResp();

        //获取订单实体
        OrderEntity orderEntity = orderEntityMapper.selectByPrimaryKey(orderId);

        //获取发布需求实体
        RequirementEntity requirementEntity =
                requirementEntityMapper.selectByPrimaryKey(orderEntity.getRequirementId());

        //服务商实体
        SupplierEntity supplierEntity = supplierEntityMapper.selectByPrimaryKey(orderEntity.getSupplierId());
        //服务商会员实体
        MemberEntity memberEntity =
                memberEntityMapper.selectByPrimaryKey(supplierEntity.getMemberId());
        //对接项目专员信息
        GetUserListResp userInfo =
                userInfoMapper.getUserInfo(orderEntity.getConnectUserId());

        OrderLogisticsEntity orderLogisticsEntity = new OrderLogisticsEntity();
        orderLogisticsEntity.setOrderId(orderId);

        //获取订单物流信息
        GetOrderLogisticsResp getOrderLogisticsResp =
                orderLogisticsInfoMapper.selectOrderLogisticsInfo(orderLogisticsEntity);

        GetOrderCommentListResp purchaserComment =
                orderCommentInfoMapper.getOrderInfoByOrderIdShowPurchaser(orderId);


        PurchaserEntity purchaserEntity1 =
                purchaserEntityMapper.selectByPrimaryKey(requirementEntity.getPurchaserId());

        //采购商会员信息
        MemberEntity purchaserEntity =
                memberEntityMapper.selectByPrimaryKey(purchaserEntity1.getMemberId());
        getExcellentCaseResp.setCaseName(requirementEntity.getDescription());
        getExcellentCaseResp.setUserName(userInfo.getRealName());
        getExcellentCaseResp.setSupplierName(memberEntity.getNickName());
        getExcellentCaseResp.setOnOrderTime(orderEntity.getCreateTime());
        getExcellentCaseResp.setDeliveryOrderTime(getOrderLogisticsResp.getCreateTime());
        getExcellentCaseResp.setOrderDesc(requirementEntity.getDescription());
        getExcellentCaseResp.setPurchaserName(purchaserEntity.getNickName());
        getExcellentCaseResp.setStars(purchaserComment.getGrade());
        getExcellentCaseResp.setOrderCommentTime(purchaserComment.getCreateTime());
        getExcellentCaseResp.setOrderSupplierInfo(purchaserComment.getToSupplier());

        result = new Result(getExcellentCaseResp);

        return result;
    }

    /**
     * 优秀加工厂信息
     *
     * @param supplierId
     * @return
     */
    @Override
    public Result getExcellentSupplierInfo(Long supplierId) {
        Result result = null;

        //服务商实体
        SupplierEntity supplierEntity =
                supplierEntityMapper.selectByPrimaryKey(supplierId);

        //获取服务商有效的评价信息
        List<GetOrderCommentListResp> validOrderComment =
                orderCommentInfoMapper.getValidOrderComment(supplierId);

        for(int i = 0; i < validOrderComment.size(); i++) {
            OrderEntity orderEntity = orderEntityMapper.selectByPrimaryKey(validOrderComment.get(i).getOrderId());
            RequirementEntity requirementEntity = requirementEntityMapper.selectByPrimaryKey(orderEntity.getRequirementId());
            PurchaserEntity purchaserEntity = purchaserEntityMapper.selectByPrimaryKey(requirementEntity.getPurchaserId());
            MemberEntity memberEntity = memberEntityMapper.selectByPrimaryKey(purchaserEntity.getMemberId());
            if(memberEntity != null) {
                //头像id
                Long headImageAttachmentId = memberEntity.getHeadImageAttachmentId();
                AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(headImageAttachmentId);
                if(attachmentEntity != null) {
                    validOrderComment.get(i).setMemberImg(attachmentEntity.getFileName());
                }
            }
            List<String> commentList = orderCommentPictureInfoMapper.getCommentList(validOrderComment.get(i).getId());
            validOrderComment.get(i).setCommentImgs(commentList);
        }
        //会员实体
        MemberEntity memberEntity =
                memberEntityMapper.selectByPrimaryKey(supplierEntity.getMemberId());

        List list = new LinkedList();
        list.add(supplierEntity);
        list.add(memberEntity);
        list.add(validOrderComment);

        result = new Result(list);
        return result;
    }
}
